

<template>
  <div  class="coupon-wrap">
      <!--头部切换-->
      <u-tab :options="uTabList" v-on:listenTabIndex="showTabIndex"></u-tab>

      <!-- 乘客 开始  -->
      <div v-for="item in coupon"  v-if="!isNoData">

          <div class='ubean-coupons'>
              <div class="ubean-coupons-top"  :class="{'borderGary':item.exchangeCount<=0 , 'borderYellow':item.exchangeCount>0&&tabIndex!=1,'borderPink':item.exchangeCount>0&&tabIndex==1}">
                  <div class='ubean-coupon-cell flex'>
                      <div class='ubean-coupon-left'>
                          <p class="ubean-coupon-hd"  :class="{'colorGary':item.exchangeCount<=0 }">{{item.name}}</p>
                          <p class="ubean-coupon-bd"  :class="{'colorGary':item.exchangeCount<=0 }">有效期：{{item.time}}</p>
                      </div>
                      <div class='ubean-coupon-right'>
                          <p class='ubean-coupon-hd center'>消耗U豆：{{item.price}}</p>
                      </div>
                  </div>
              </div>
              <div class='ubean-coupons-bottom flex'>
                  <div class='ubean-coupon-bottom-left' v-if="item.exchangeCount>0">本周还可兑换{{item.exchangeCount}}次</div>
                  <div class='ubean-coupon-bottom-left' style='color:#999999;' v-else>下周再来兑换</div>
                  <div class='ubean-coupon-bottom-right ubean-btn'    @click="couponbtn(item.name,item.id,item.time,item.price)"  v-if="item.exchangeCount>0">立即兑换</div>
              </div>
          </div>
      </div>
      <div class="coupon-disable-tip" >限时抢购为每周日00：00刷新</div>

      <!-- 乘客 end -->

      <!-- 没有数据样式 -->
      <view class='noData' v-if="isNoData"></view>
      <text class='noDataTitle' v-if="isNoData">发放福利的时间还没到哦~</text>
      <!-- 底部弹出遮罩 -->
      <div class="input-mask" :class="{'text-show':textShow}">
          <div class="input-comment" :class="{'boxIn':textShow}">
              <div class='input-comment-box'>
                  <p class='input-comment-title'>{{input_title}}</p>
                  <img class='input-comment-out'   src='http://img.uuuda.com/welfare/close.png' @click="handleBtn" />

              </div>

              <div class="input-comment-show " >
                  <div class='show-cell'>
                      <p class='text-left'>兑换类型</p>
                      <p class='text-right'>{{coupon_type}}</p>
                  </div>
                  <div class='show-cell'>
                      <p class='text-left'>有效期</p>
                      <p class='text-right'>{{indate}}</p>
                  </div>
                  <p class='show-price'>消耗:{{coupon_old_price}}U豆</p>
                  <p class='show-u-price'>(您的账户有{{balance}}U豆)</p>
              </div>


              <div class="submit-comment" @click="cancelComment">{{submit_text}}</div>
          </div>
      </div>
      <!-- 底部弹出遮罩 end -->
  </div>

</template>

<script>
    import uTab from "../Commons/tab/index"

    let axios = require("axios");
    //总按钮锁 不让点
    let btnLock = true;
    let token='';
    export default {
        components: {
            "u-tab":uTab

        },


        data() {
            return {
                submit_text: "确认兑换",
                input_title: "兑换优惠券",
                textShow: false,
                coupon: [],
                coupon2:[],
                balance:0,//余额
                coupon_type: "",//券类型
                indate: "",//有效期
                coupon_old_price: 0,//消耗U豆
                coupon_id:0,
                userInfo: { vehicle: {} },
                taskPercent:0,
                vehicleStatus:'',
                isNoData:false,//是否有数据
                uTabList:{
                    tabList:[
                        {index:0,name:'拼车优惠券'},
                        {index:1,name:'洗车优惠券'},
                    ]
                },
                tabIndex:0,
            }
        },
        created() {
        },
        mounted(){

           this.initPage();
        },
        watch:{
        },
        methods: {

            //初始化页面
            initPage(){


                let self = this;
                let query = this.$route.query;
                // let token = query.u || '';
                  token ="d4bca1d1911be6ed4ed1a077996ba6842331c8ca184c737af3a0a0357b899f1c";

                self.getCouponList('coupon1');


            },
            //头部切换点击
            showTabIndex:function(data){
                let self=this;
                console.log('接受的值')
                console.log(data)
                this.tabIndex=data
                if(data==0){
                    self.getCouponList('coupon1');
                }else{
                    self.getCouponList('coupon2');

                }
            },
            //请求換优惠券接口
            getCouponList(name) {
                let self = this;
                //1. 优惠券列表接口
                self.coupon=[]//清空数据
                self.uTabList={
                    tabList:[
                        {index:0,name:'拼车优惠券'},
                        {index:1,name:'洗车优惠券'},
                    ]
                },

                axios({
                    method: "POST",
                    url: "/api/ubeans/exchangelist",
                    params: {
                      'token':token,
                    }
                }).then(function(res) {
                    let rdata = res.data;
                    if(rdata.errno == 200){
                        let rdata = res.data.data;

                        switch (name) {
                            case 'coupon1':
                                self.coupon= self.getDataGroup(rdata.carpool_truck_coupon_data)

                                break;
                            case 'coupon2':
                                self.coupon= self.getDataGroup(rdata.car_wash_data)
                                break;
                        }
                        self.balance= rdata.balance

                        console.log(self.coupon)
                    }else {
                        self.$dialog.toast({
                            mes: res.data.msg,
                            timeout: 1500
                        });
                    }
                });

            },

            getDataGroup:function (dataO) {
                let couponData = [];
                let self = this;
                if (dataO.length > 0) {
                    for (let i = 0; i < dataO.length; i++) {
                        couponData[i] = {
                            name: dataO[i].name,
                            time: dataO[i].timeRestrictions + "天",
                            price: dataO[i].price,
                            oldPrice: dataO[i].oldPrice,
                            coupon_type: dataO[i].type,
                            id: dataO[i].id,
                            exchangeCount: parseInt(dataO[i].numberRestrictions) - parseInt(dataO[i].buyNumber)
                        }
                    }
                    return couponData;
                }else {
                    self.isNoData=true
                }
            },
            //立即兌換按鈕
            couponbtn:function(name,coupon_id,time,price){
                    let self=this;
                    self.textShow= true,
                    self.coupon_type= name,
                    self.indate= time,
                    self.coupon_old_price=price,
                    self.coupon_id= coupon_id
            },
            //返回和关闭
            handleBtn: function (e) {
                let self = this;
                btnLock = true;
                self.textShow= false;

            },
            //兑换按钮
            cancelComment: function () {
                let self = this;
                if (self.balance < self.coupon_old_price) {

                    self.$dialog.toast({
                        mes: '您的U豆不足',
                        timeout: 1500
                    });
                    return;
                }
                self.submit_text="兑换中..."

                if (btnLock) {
                    btnLock = false;
                    self.beanExchange(self.coupon_id);
                }
            },
            //换优惠券请求接口
            beanExchange: function (coupon_id){
                let self=this;
                axios({
                    method: "POST",
                    url: "/api/ubeans/exchange",
                    params: {
                      'token':token,
                      'id': coupon_id
                    }
                }).then(function(res) {
                    let rdata = res.data.data;

                    if(res.data.errno == 200){

                        btnLock = true
                        self.$dialog.toast({
                            mes: '兑换成功',
                            timeout: 1500
                        });
                            self.textShow=false,
                            self.submit_text="确认兑换"
                            self.getCouponList('coupon1');//初始化券数据

                    }else {
                        btnLock = true
                        self.$dialog.toast({
                            mes: res.data.msg,
                            timeout: 1500
                        });
                        self.textShow=false,
                        self.submit_text="确认兑换"
                        self.getCouponList('coupon1');//初始化券数据

                    }


                });

            },



        }
    }
</script>

<style>

    .coupon-wrap{
        padding-top: 60px;
        padding-bottom: 20px;
    }

    /* 底部优惠券 */
    .ubean-coupons{
        position: relative;
        background: white;
        width:351px;
        margin: 0 auto 12px auto;

    }
    .coupon-disable-tip{
        font-size:12px;
        color:#808080;
        text-align:center;
        width:350px;
        margin:30px 0 10px 0;

    }
    .ubean-coupons-top{
        position: relative;
        width: 351px;
        /* background:radial-gradient(100px 100px at left bottom,transparent 50%,#e5e5e5 50%); */
        box-shadow:
                -1.5px 0 1.5px -1px #e5e5e5,
                0 -1.5px 1.5px -1px #e5e5e5,
                0 0px 0px 0px white,
                1.5px 0 1.5px -1px #e5e5e5;
        border-top-left-radius: 4px;
        border-top-right-radius: 4px;
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;
        margin: 0 auto;
    }
    .ubean-coupon-cell{
        width: 90%;
        margin-left: 12px;
        margin-bottom: 12px;
        padding: 8px;

    }

    .ubean-coupon-dashed{
        width: 1px;
        height: 52px;
        border-right: 2px #FFCD00 dashed;
        position: absolute;
        left:57px;
        top:6px;
    }
    .flex{
        display:-webkit-box;
        display:-webkit-flex;
        display:flex;
    }
    .ubean-coupon-left{
        flex: .6;
    }
    .ubean-coupon-right{
        flex: .4;
    }
    .ubean-coupon-left text{
        display: block;
    }
    .ubean-coupon-right text{
        display: block;
    }
    .center{
        text-align: center;
    }
    .ubean-coupon-hd{
        font-size: 18px;
        color: #1A1A1A;
        font-weight: bold;
    }
    .ubean-coupon-bd{
        font-size: 12px;
        color: #1A1A1A;
        margin-top: 4px;
    }
    .line-through{

        text-decoration:line-through;
        color: #999999;
    }
    .ubean-dashed-box{
        position: relative;
        width: 360px;
        height: 15px;
        margin: 0 auto;
    }
    .ubean-coupons-bottom{
        position: relative;
        width: 351px;
        height: 50px;
        box-shadow:
                -1.5px 0 1.5px -1px #e5e5e5,
                0 0px 0px 0px white,
                0 1.5px 3px -1px #e5e5e5,
                1.5px 0 1.5px -1px #e5e5e5;
        border-top-left-radius: 4px;
        border-top-right-radius: 4px;
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;
        margin: 0 auto;
    }
    .ubean-coupon-bottom-left{
        flex: 1;
        font-size:12px;
        color: #FF4C6A;
        line-height: 50px;
        margin-left: 16px;
    }
    .ubean-btn{
        position: relative;
        top:50%;
        margin-top: -13px;
        height: 26px;
        font-size:12px;
        color: #4B0C0C;
        line-height:26px;
        background: #FFCD00;
        padding: 0 5px;
        border-radius: 2px;
        margin-right: 12px;
    }
    /* 底部优惠券 end*/

    /* 置灰样式 */
    .borderGary{
        border-bottom: 2px #999999 dashed;
    }
    .borderYellow{
        border-bottom: 2px #FFCD00 dashed;
    }
    .colorGary{
        color: #999999!important;
    }
    .borderPink{
        border-bottom: 2px #FF4C6A dashed
    }

    /* 限时抢购title */
    .ubean-time-limit{
        position: relative;
        width: 100%;
        background: #f5f5f5;
        overflow: hidden;
        padding: 12px 0;
    }
    .ubean-time-limit p:first-child{
        font-size:18px;
    =        color: #1a1a1a;
        font-weight: bold;
    }
    .ubean-time-limit p{
        display: block;
        font-size: 12px;
        color: #999999;
        padding-left: 12px;

    }
    /* 遮罩相关 */
    .input-mask{
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        top: 0;
        background:rgba(0, 0, 0, 0.4);
        display: none;
    }
    .input-mask.text-show{
        display: block;
    }
    .wrap-submit-comment{
        margin-top: 8px;
        z-index: 9999;
    }
    .submit-comment{
        font-size:16px;
        color:white;
        height:44px;
        line-height:44px;
        font-weight: bold;
        background:#55BCFF;
        text-align:center;
        position:absolute;
        width:327px;
        bottom: 12px;
        border-radius: 22px;
        left:50%;
        margin-left:-164px;

    }
    .input-comment{
        height: 343px;
        position: fixed;
        z-index: 9999;
        bottom: 0;
        left: 0;
        right: 0;
        top:100%;
        background-color: #fff;
        border-top-left-radius: 8px;
        border-top-right-radius: 8px;
    }

    .input-comment textarea{
        margin: 8px 6px 0px 6px;
        height: 36px;
        background-color: #f5f5f5;
        padding: 6px 4px 0px 4px;
        box-sizing: border-box;
        width: 100%;
    }
    .boxOut{
        -webkit-animation:boxOut 50s ease forwards;
        animation:boxOut 50s ease forwards;

    }
    @-webkit-keyframes boxOut{
        from {
            -webkit-transform: translateY(-100%);
        }
        to {
            -webkit-transform: translateY(0%);
        }
    }
    .boxIn{
        -webkit-animation:boxIn 0.5s ease forwards;
        animation:boxIn 0.5s ease forwards;

    }
    @-webkit-keyframes boxIn{
        from {
            -webkit-transform: translateY(0%)
        }
        to {-webkit-transform: translateY(-100%)
        }
    }
    .input-comment-show{
        width:100%;
        position:relative;
    }

    .input-comment-box{
        position:relative;
        display:-webkit-flex;
        display:flex;
        border-bottom:1px solid #e6e6e6;
        padding:17px 0;

    }

    .input-comment-title{
        flex:1;
        text-align:center;
        font-size: 18px;
        font-weight: bold;
    }
    .input-comment-back{
        width: 20px;
        height:20px;

        margin-left:5%;
    }
    .input-comment-out{
        width:20px;
        height:20px;
        margin-right:5%;

    }
    .show-cell{
        position:relative;
        height:52px;
        width:90%;
        border-bottom:2px solid #e6e6e6;
        margin:0 auto;
        line-height:52px;
    }
    .text-left{
        position:absolute;
        font-size: 16px;
        left:0;
        color: #666666;
    }
    .text-right{
        position:absolute;
        font-size: 16px;
        right:0;
        font-weight: bold;
        color: #1a1a1a;
    }
    .show-price{
        position:absolute;
        font-size:16px;
        right:20px;
        margin-top:20px;
        font-weight:bold;
        color:#1a1a1a;
        display:block;
    }
    .show-u-price{
        position:absolute;
        font-size:12px;
        right:20px;
        margin-top:52.5px;
        color:#666666;
        display:block;
    }
    /* 车主样式 */
    .owner-head{
        border-bottom: 1px solid #ededed;

    }
    .owner-head-left p{
        display: block;
        text-align: center;
        font-size: 12px;
        color: #1a1a1a;
        padding-bottom: 33px;
    }
    .owner-head-left img{
        width: 60px;
        height: 60px;
        padding: 34px 12px 8px 12px;
    }
    .owner-title{
        font-size: 16px;
        color: #1a1a1a;
        font-weight: bold;
        margin-top: 16px;
        margin-bottom: 8px;

    }
    .owner-sub{
        position: relative;
        font-size: 14px;
        color: #666666;
        margin-bottom: 8px;
        padding-right: 12px;
    }
    .owner-dec{
        position: relative;
        font-size: 12px;
        color: #999999;
        padding-bottom: 8px;
        padding-right: 12px;

    }
    .taskBtn{
        text-align: center;
        font-size: 16px;
        color: #1A9AFF;
        padding: 12px 0;
    }

    .taskBox{
        /* background: lightblue; */
        padding-left: 12px;
        position: relative;
    }
    .task-title{
        font-size: 16px;
        color: #1a1a1a;
        font-weight: bold;
        padding-top: 16px;
        display: block;
        padding-bottom: 4px;
    }

    .task-time{
        position: relative;
        font-size: 12px;
        color: #666666;
        display: block;
        padding-bottom: 22px;

    }
    .task-logo{
        position: absolute;
        right: 12px;
        top:6px;
        width: 54px;
        height: 54px;
    }
    .task-progress-box{
        position: relative;
        margin-bottom: 29px;
    }
    .task-progress{
        width: 200px;
        height: 11px;
        /* background: #ededed; */
        /* border-radius: 11px; */
        margin-right: 16px;
    }
    .task-order-status{
        display: inline-block;
        font-size: 14px;
        line-height: 12.5px;
        color: #1a1a1a;
    }
    .task-gray-man{
        background: url('http://img.uuuda.com/mine/task-gray-man.png')0 0 no-repeat;
        background-size: 100% 100%;
        width: 18px;
        height: 22px;
        display: inline-block;
        margin-right: 16px;
    }
    .task-blue-man{
        background: url('http://img.uuuda.com/mine/task-blue-man.png')0 0 no-repeat;
        background-size: 100% 100%;
        width: 18px;
        height: 22px;

    }
    .task-progress-logo{
        padding-bottom: 24px;
    }
    .task-progress-status{
        font-size: 14px;
        line-height: 23px;
        color: #1a1a1a;
    }
    .owner-head-right{
        flex: 1;
    }

    /* 遮罩结束 */
    /* 没有数据样式 */
    .noDataTitle{
        position: relative;
        text-align: center;
        display: block;
        color: #808080;
        font-size: 14px;
        letter-spacing:1px;
    }
    .taskNoData{
        position: relative;
        display: block;
        width: 160px;
        height: 147px;
        margin: 0 auto;

    }

</style>


